package com.baidu.videoseries.activity;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.SeekBar;

import com.baidu.videoseries.R;
import com.baidu.videoseries.constant.Config;
import com.baidu.videoseries.receiver.MusicReceiver;
import com.baidu.videoseries.util.LogUtils;

import org.simple.eventbus.EventBus;

public class PlayDetailsActivity extends AppCompatActivity implements MusicReceiver.OnReceiverListener, SeekBar.OnSeekBarChangeListener {
    private static final String TAG = "PlayDetailsActivity";
    private android.widget.SeekBar musicSeek;
    private MusicReceiver musicReceiver;
    private boolean isFirst = true;

    private EventBus mEventBus;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play_details);
        this.musicSeek = (SeekBar) findViewById(R.id.music_seek);
        initView();
    }

    private void initView() {
        //register eventBus
        this.mEventBus = EventBus.getDefault();
        this.mEventBus.register(this);

        this.musicSeek.setMax(100);
        this.musicSeek.setOnSeekBarChangeListener(this);
        musicReceiver = new MusicReceiver();
        musicReceiver.setOnReceiveListener(this);
        registerReceiver(musicReceiver, new IntentFilter(Config.MediaAction.ACTION_PREPARED));
    }

    @Override
    protected void onDestroy() {
        unregisterReceiver(musicReceiver);
        mEventBus.unregister(this);
        super.onDestroy();
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent == null) return;
//        if (isFirst) {
//            musicSeek.setMax(intent.getIntExtra(Key.MUSIC_LENGTH, 100));
//            isFirst = false;
//        }
        musicSeek.setProgress(intent.getIntExtra(Config.Key.PROGRESS, 1));
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        LogUtils.e("-----" + progress);
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        LogUtils.e(TAG, "---stopTrack");
        mEventBus.post(seekBar.getProgress(), Config.Tags.AUDIO_NEW_PROGRESS);
    }
}
